package com.amex.uam.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.amex.uam.dao.DeptDao;
import com.amex.uam.model.USER_DEPT;

@Repository("deptDao")
public class DeptDaoImpl extends JdbcDao implements DeptDao {

	@Override
	public boolean addDepartment(USER_DEPT dept) {
		throw new RuntimeException("DeptDaoImpl.deleteDepartment not implemented");
	}

	@Override
	public boolean deleteDepartment(String deptId) {
		// TODO Auto-generated method stub
		throw new RuntimeException("DeptDaoImpl.deleteDepartment not implemented");
	}

	@Override
	public boolean updateDepartment(USER_DEPT dept) {
		final String sql= "select DEPT_ID, DEPT_NM from USER_DEPT where DEPT_ID = ?";
		final String insertSql= "insert into USER_DEPT(DEPT_ID, DEPT_NM) values(?, ?)";
		final String updateSql= "update USER_DEPT set DEPT_NM = ? where DEPT_ID = ?";
		try {
			
			Object[] params= {dept.getDEPT_ID()};
			RowMapper<USER_DEPT> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(USER_DEPT.class);
			List<USER_DEPT> list = this.getJdbcTemplate().query(sql, params, rowMapper);
			if (list == null || list.size() == 0) {
				Object[] insertParams={dept.getDEPT_ID(), dept.getDEPT_NM()};
				this.getJdbcTemplate().update(insertSql, insertParams);
			}else{
				Object[] updateParams={dept.getDEPT_NM(), dept.getDEPT_ID()};
				this.getJdbcTemplate().update(updateSql, updateParams);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public USER_DEPT getDepartment(String deptId) {
		// TODO Auto-generated method stub
		throw new RuntimeException("DeptDaoImpl.deleteDepartment not implemented");
	}

}
